package com.wytec.xywzxw.service.impl;

import com.wytec.xywzxw.constant.MyConstant;
import com.wytec.xywzxw.service.VerifyCodeService;
import com.wytec.xywzxw.utils.EmailUtils;
import com.wytec.xywzxw.utils.redis.RedisUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

@Service
public class VerifyCodeServiceImpl implements VerifyCodeService {
    @Value("${email.user}")
    private String sendUser;

    @Value("${custom.verify_expired}")
    private Integer expired;

    @Resource
    RedisUtils redisUtils;

    @Override
    public Boolean sendEmailVerifyCode(String code,String email, int type) {

        //	创建Session会话
        Session session = EmailUtils.createSession();
        //	创建邮件对象
        MimeMessage message = new MimeMessage(session);
        try {
            String body = emailTemplate(code,type);
            message.setSubject("邮箱验证");

            message.setFrom(new InternetAddress(sendUser));
            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(email));

            //	邮件主体
            BodyPart textPart = new MimeBodyPart();
            textPart.setContent(body, "text/html;charset=utf-8");
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(textPart);

            //	将邮件装入信封
            message.setContent(multipart);

            //	发送
            Transport.send(message);

            // 存入缓存
            redisUtils.set(MyConstant.VERIFY +":"+email,code,expired);

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public Boolean sendMobileVerifyCode(String code,String mobile, int type) {
        // 存入缓存
        redisUtils.set(MyConstant.VERIFY +":"+mobile,code,expired);
        return true;
    }

    @Override
    public Boolean checkCode(String code, String account) {
        String key = MyConstant.VERIFY + ":" + account;
        String value = redisUtils.get(key);
        if (value == null || value.equals("")){
            return false;
        }
        Boolean res = code.equals(value);
        if (res){
            redisUtils.del(key);
        }
        return res;
    }


    private String emailTemplate(String code,int type){

//        File file = new File("src/main/resources/help.html");
        File file = new File("src/main/resources/ftl/email_verify_template.ftl");
        String temp = "";
        try {
            FileReader fileReader = new FileReader(file);
            BufferedReader reader = new BufferedReader(fileReader);
            String line;
            while ((line = reader.readLine()) != null) {
                // 处理每一行的数据
                temp = temp + line;
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        temp = temp.replace("{{code}}",code);
        temp = temp.replace("{{project_name}}","飓风逍遥");
        temp = temp.replace("{{expired}}",String.valueOf((expired / 60)));
        temp = temp.replace("{{send_user}}","张晓伟");

        return temp;
    }
}
